<?php

namespace common\models;

use sjaakp\taggable\TagBehavior;
use Yii;

/**
 * This is the model class for table "tag".
 * @property integer $id
 * @property string $name
 * @property integer $count
 */
class Tag extends \yii\db\ActiveRecord
{
    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'tag' => [
                'class' => TagBehavior::className(),
                'junctionTable' => 'tag_and_article',
            ]
        ];
    }

    public function getArticles()
    {
        return $this->hasMany(Article::className(),['id'=>'article_id'])->viaTable('tag_and_article',['tag_id'=>'id']);
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tag';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['count'], 'integer'],
            [['name'], 'string', 'max' => 10],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '标签',
            'count' => '次数',
        ];
    }

    public function getArtilces()
    {
        return $this->hasMany(Article::className(), ['id' => 'article_id'])->viaTable('article_and_category', ['category_id' => 'id'])->inverseOf('tags');
    }
}
